<?php
defined('ROOTPATH') OR exit('No direct script access allowed');
/**
 * cache
 */
class Cache
{
	static public $_instance = null;

	static public function getInstance()
    {
		if (!self::$_instance)
        {
			$cache_path = APP_PATH.'cache'.DIRECTORY_SEPARATOR;
			if (!file_exists($cache_path)) {
				@mkdir(dirname($cache_path), 0777, true);
				// file_put_contents(this->cache_path, '');
			}
			self::$_instance = new \Doctrine\Common\Cache\FilesystemCache($cache_path);
		}

		return self::$_instance;
	}

	/**
	 * 写缓存
	 * @param  string  $key      缓存key
	 * @param  [type]  $data     缓存的数据
	 * @param  integer $lifeTime 保存的时间false为永久
	 * @return [type]            boolean
	 */
	static public function write($key = '', $data = null, $lifeTime = false)
    {
		return self::getInstance()->save($key, $data, $lifeTime);
	}

	/**
	 * 写临时缓存
	 * @param  string $key 缓存key
	 * @return [type]      [description]
	 */
	static public function writeTemp($key = '', $data = null, $lifeTime = false)
    {
		return self::getInstance()->save($key, $data, $lifeTime);
	}

	/**
	 * 读缓存
	 * @param  string $key 缓存key
	 * @return [type]      [description]
	 */
	static public function read($key = '')
    {
		return self::getInstance()->fetch($key);
	}

	/**
	 * 删除缓存
	 * @param  string $key 缓存key
	 * @return [type]      [description]
	 */
	static public function delete($key = '')
    {
		return self::getInstance()->delete($key);
	}

	/**
	 * 检查缓存是否存在
	 * @param  string $key [description]
	 * @return [type]      [description]
	 */
	static public function exists($key = '')
    {
		return self::getInstance()->contains($key);
	}

	/**
	 * 删除全部缓存
	 * @param  string $key 缓存key
	 * @return [type]      [description]
	 */
	static public function deleteAll()
    {
		return self::getInstance()->deleteAll();
	}

	/**
	 * 析构方法
	 * @access public
	 */
	public function __destruct() {
        //
	}
}
